import datetime

from src.application.user import dto
from src.application.user.uow import UserUoW
from src.application.user.use_cases import (
    CreateUser,
    CreateUserHandler,
    GetUserById,
    GetUserByIdHandler,
    GetUsers,
    GetUsersHandler,
    RegisterUser,
    RegisterUserHandler,
    SetTestProxyAcquired,
    SetTestProxyAcquiredHandler,
)
from src.application.user.use_cases.change_user_language import ChangeUserLang, ChangeUserLangHandler
from src.application.user.use_cases.create_allowed_ip import CreateAllowedIp, CreateAllowedIpHandler
from src.application.user.use_cases.get_user_by_tg_id import GetUserByTgId, GetUserByTgIdHandler


class UserService:
    def __init__(
        self,
        *,
        uow: UserUoW,
    ) -> None:
        self._uow = uow

    async def create_user(self, tg_id: int, username: str) -> dto.UserDTO:
        """Сохранение данных пользователя в бд"""

        command = CreateUser(tg_id=tg_id, username=username)
        return await CreateUserHandler(uow=self._uow)(command=command)

    async def register_user(self, tg_user_id: int, email: str) -> dto.UserDTO:
        """Регистрация пользователя"""

        command = RegisterUser(tg_user_id=tg_user_id, email=email)
        return await RegisterUserHandler(uow=self._uow)(command=command)

    async def get_user_by_id(self, user_id: int) -> dto.UserDTO:
        """Получение полных данных о пользователе по id"""

        command = GetUserById(user_id=user_id)
        return await GetUserByIdHandler(uow=self._uow)(command=command)

    async def get_user_by_tg_id(self, user_tg_id: int) -> dto.UserDTO:
        """Получение полных данных о пользователе по telegram id"""

        command = GetUserByTgId(user_tg_id=user_tg_id)
        return await GetUserByTgIdHandler(uow=self._uow)(command=command)

    async def get_users(self, query: GetUsers) -> dto.Users:
        """Получение пользователей с фильтрами"""

        return await GetUsersHandler(uow=self._uow)(query=query)

    async def set_test_proxy_acquired(self, tg_user_id: int, test_proxy_acquired: datetime.datetime) -> dto.UserDTO:
        """Обновление даты получения тестового прокси"""

        command = SetTestProxyAcquired(tg_user_id=tg_user_id, test_proxy_acquired=test_proxy_acquired)
        return await SetTestProxyAcquiredHandler(uow=self._uow)(command=command)

    async def change_user_lang(self, tg_user_id: int, lang_code: str) -> None:
        """Меняем язык пользователя"""

        command = ChangeUserLang(tg_user_id=tg_user_id, lang_code=lang_code)
        return await ChangeUserLangHandler(uow=self._uow)(command=command)

    async def create_allowed_ip(self, tg_user_id: int, ip_address: str) -> None:
        """Создание allowed_ip"""

        command = CreateAllowedIp(tg_user_id=tg_user_id, ip_address=ip_address)
        return await CreateAllowedIpHandler(uow=self._uow)(command=command)
